<?php
//获取请求必传参
$actionData = $route->bodyParams['actionData'];
$menuId     = $route->bodyParams['menuId'];

//关闭事物自动提交
$db->mysqlDB->falseAutoCommit();

//删除原有的角色
$deleteParams = array(
    "delete_where"       => "mapm_mid",
    "mapm_mid"           => $menuId,
    "isPhysicallyDelete" => "1"
);

//删除原有角色
$R1 = $db->mysqlDB->automaticDelete($deleteParams)->delete("sys_menu_action_permissions_middle");

if(count($actionData) == 0){
    if(!$R1){
        $db->mysqlDB->rollBackCommint();
        $response->responseData(false);
    }else{
        $db->mysqlDB->actionCommint();
        $db->mysqlDB->trueAutoCommit();
        $response->responseData(true);
    }
}

//拼接多新增语句SQL
$addSql = "INSERT INTO sys_menu_action_permissions_middle";

$keys = "(mapm_mid,mapm_apid) VALUES";

$values = "";

for($i=0;$i<count($actionData);$i++){

    $apid = trim($actionData[$i]['ap_id']);

    $tmpValue = "(";
    $tmpValue .= $menuId . ",'" . $apid . "'";
    $tmpValue .= "),";

    $values .= $tmpValue . "";

}

//拼接最终SQL
$addSql .= $keys .= rtrim($values, ",");

//执行SQL
$R2 = $db->mysqlDB->queryTransaction($addSql);

//判断两个操作是否执行成功
if (!$R1 || !$R2) {
    $db->mysqlDB->rollBackCommint();
    $response->responseData(false);
}else{
    $db->mysqlDB->actionCommint();
    $db->mysqlDB->trueAutoCommit();
    $response->responseData(true);
}